package com.tarry.queue;

import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 基于链表实现队列
 * @param <T>
 */

@Slf4j
public class LinkedListQueue<T> {
    // 出队列指针
    private Node head;
    // 入队列指针
    private Node tail;

    /**
     * 入队列
     * @param item
     * @return
     */
    public boolean enQueue(T item){
        Node newNode = new Node();
        newNode.setData(item);
        if (Objects.isNull(head)) {
            head = newNode;
        }
        if (Objects.isNull(tail)) {
            tail = new Node();
        }
        tail.setNext(newNode);
        tail = tail.getNext();
        return true;
    }

    /**
     *  出队列
     * @return
     */
    public T deQueue(){
        if (Objects.isNull(this.head)) {
            log.warn("the queue is empty");
            return null;
        }
        T item = (T) this.head.getData();
        this.head = this.head.getNext();
        return item;
    }

    /**
     * 显示队列中队元素
     */
    public void showQueue() {
        if (Objects.isNull(this.head)) {
            log.warn("the queue is empty");
            return;
        }
        Node item = this.head;
        while (item.getNext() != null){
            System.out.println(item.getData());
            item = item.getNext();
        }
    }

}
